﻿using System;
using System.Data.Common;
using System.Data;
using System.Collections.Generic;
using LearnNet.Entity;
namespace LearnNet.DBUtility
{
    /// <summary>
    /// 获取记录值
    /// </summary>
    /// <param name="dr"></param>
    /// <returns></returns>
    public delegate object PopulateDelegate(IDataReader dr);
    /// <summary>
    /// 委托将DataReader转为实体类
    /// </summary>
    /// <param name="dr">记录集</param>
    /// <param name="Fileds">字段名列表</param>
    /// <returns></returns>
    public delegate T PopulateDelegate<T>(IDataReader dr, Dictionary<string, string> Fileds);

    /// <summary>
    /// 自定义数据库类型
    /// </summary>
    public enum MyDBType
    {
        Int = 0,
        Decimal,
        Long,
        Varchar,
        Nvarchar,
        DateTime,
        Text,
        Image,
        TinyInt,
        NText
    }

    interface IDBHelper
    { 
        /// <summary>
        ///  返回DBComd参数
        /// </summary>
        /// <param name="ParaName">参数名</param>
        /// <param name="DataType">参数类型 字符串形式</param>
        /// <returns></returns>
        DbParameter GetParameter(string ParaName, MyDBType DataType, int size, object value, ParameterDirection direction);


      
        /// <summary>
        /// 获取分页SQL
        /// </summary>
        /// <param name="qp">查询条件类</param>
        /// <returns></returns>
        string GetPagerSQL(QueryParam qp);



        /// <summary>
        /// 执行 Transact-SQL 语句并返回受影响的行数。
        /// </summary>
        int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

        /// <summary>
        /// 在事务中执行 Transact-SQL 语句并返回受影响的行数。
        /// </summary>
        int ExecuteNonQuery(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

        /// <summary>
        /// 在事务中执行查询，返回DataTable
        /// </summary>
        DataTable ExecuteQuery(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

        /// <summary>
        /// 执行查询，返回DataTable
        /// </summary>
        DataTable ExecuteQuery(string connectionString, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

        /// <summary>
        /// 在事务中执行查询，返回DataReader
        /// </summary>
        DbDataReader ExecuteReader(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

        /// <summary>
        /// 执行查询，返回DataReader
        /// </summary>
        DbDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

        /// <summary>
        /// 在事务中执行查询，并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
        /// </summary>
        object ExecuteScalar(DbTransaction trans, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

        /// <summary>
        /// 执行查询，并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
        /// </summary>
        object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params DbParameter[] cmdParms);

          /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="qp">查询分页类</param>
        /// <param name="connectionString">连接字符串</param>
        /// <returns></returns>
        DbDataReader GetPageList(QueryParam qp, string connectionString);

        /// <summary>
        /// 得到数据条数
        /// </summary>
        /// <param name="tblName">表名</param>
        /// <param name="condition">条件(不需要where)</param>
        /// <returns>数据条数</returns>
        int GetCount(string connectionString, string tblName, string condition);

         /// <summary>
        /// 公共查询数据函数Oracle版(非安全函数,传入参数请进行Sql字符串过滤)
        /// </summary>
        /// <param name="pd">委托对象</param>
        /// <param name="pp">查询字符串</param>
        /// <param name="pp">是否返回总数</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>返回记录集List</returns>
        List<T> GetRecordList<T>(PopulateDelegate<T> pd, QueryParam pp, bool isReturnCount, out int RecordCount, string connstring);
    }
}
